package com.zh.webflux.config;

import com.zh.webflux.web.HelloHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.contentType;

/**
 * 定义URL的路由
 *
 * @author ZH
 * @date 20:58 2019/11/15
 */
@Configuration
public class DefineRouter {

    @Bean
    RouterFunction<ServerResponse> helloRouter(HelloHandler handler) {
        return RouterFunctions.nest(RequestPredicates.path("/hello")
                , RouterFunctions
                        .route(GET("/echo"), handler::echo)
                        .andRoute(GET("/params").and(contentType(MediaType.ALL)), handler::params)
                        .andRoute(POST("/form-data").and(contentType(MediaType.MULTIPART_FORM_DATA)), handler::formData)
                        .andRoute(POST("/form-urlencoded").and(contentType(MediaType.APPLICATION_FORM_URLENCODED)), handler::formUrlencoded)
                        .andRoute(POST("/body-json").and(contentType(MediaType.APPLICATION_JSON)), handler::bodyJson)
        );
    }

}
